package cn.sdormitory.job.dormitory;

import cn.sdormitory.basedata.dao.BDormitoryDao;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.common.sms.TxSmsTemplate;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.sys.dao.SysDictDetailDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDictDetail;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.http.client.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.*;

/**
 * @author: lx
 * @date: 2023/9/13 16:15
 * @description: 宿舍考勤通知宿管老师Job：各个宿舍未归寝的数量
 */
public class DormitoryAttendanceNoticeDorTeacherJob extends QuartzJobBean {

    @Autowired
    private BDormitoryDao bDormitoryDao;

    @Autowired
    private SdAttenceDao sdAttenceDao;

    @Autowired
    private SysDictDetailDao sysDictDetailDao;

    @Autowired
    private TxSmsTemplate txSmsTemplate;

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date = new Date();
        String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");

        //查询共有多个宿舍
        LambdaQueryWrapper<BDormitory> dormitoryWrapper = new LambdaQueryWrapper<>();
        dormitoryWrapper.eq(BDormitory::getStatus,"1");
        List<BDormitory> dormitoryList = bDormitoryDao.selectList(dormitoryWrapper);

        //宿舍楼栋
        LambdaQueryWrapper<SysDictDetail> dictWrapper = new LambdaQueryWrapper<>();
        dictWrapper.eq(SysDictDetail::getDictType,"sys_building")
                    .eq(SysDictDetail::getValid,1);
        List<SysDictDetail> sysDictDetails = sysDictDetailDao.selectList(dictWrapper);

        //短信内容
        Map<String,StringBuilder> smsContent = new HashMap<>();
        for(SysDictDetail item : sysDictDetails){
            StringBuilder sb = new StringBuilder(item.getDictLabel()).append("：");
            smsContent.put(item.getDictValue(),sb);
        }

        //拼接短信字符串
        for(BDormitory item : dormitoryList){
            int notReturnToBed = sdAttenceDao.getNotReturnToBed(accessDate,item.getId());
            if(notReturnToBed == 0){
                continue;
            }
            smsContent.get(item.getBuildingNo()).append(item.getDormitoryNo()).append("—").append(notReturnToBed).append("人").append("，");
        }

        //如果不存在未归寝的学生数据，则不进行短信发送(字符串长度如果没有改变则判断为不需要发送短信)
        int flag = 0;
        for(SysDictDetail item : sysDictDetails){
            StringBuilder sb = smsContent.get(item.getDictValue());
            StringBuilder target = new StringBuilder(item.getDictLabel()).append("：");
            if(sb.length() == target.length()){
                flag ++;
            }
        }
        if(flag == sysDictDetails.size()){
            return;
        }

        //去掉短信末尾，改为。
        StringBuilder dorTeacherContent = new StringBuilder();
        Set<String> keySet = smsContent.keySet();
        for(String key : keySet){
            dorTeacherContent.append(smsContent.get(key).substring(0,smsContent.get(key).length()-1)).append("。");
        }

        //发送短信
        String[] params = {accessDate,dorTeacherContent.toString()};

//        txSmsTemplate.sendMesModel("18252606788", params, 3);

        //获取所有宿舍老师，给每一位宿管老师发送短信通知提醒
        List<SysUser> dorTeacher = sysUserDao.getUserListByRole("dorTeacher");
        for(SysUser sysUser : dorTeacher){
            if(sysUser.getPhone() != null){
                txSmsTemplate.sendMesModel(sysUser.getPhone(), params, 3);
            }
        }
    }


}
